package Ep07_BackTrack.Back4_0216;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jimmy Zhan WORKSTATION
 * @date 2023/7/10 14:01
 * 说明：216. 组合总和 III
 */

public class Mine {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {  // k是递归层数，n是相加求和的结果
        traversal(k, n, 1, 0);
        return res;
    }
    private void traversal(int k, int n, int start, int sum) {
        if (path.size() == k) {
            if (sum == n) {
                res.add(new ArrayList(path));
            }
            return;
        }
        for (int i = start; i <= 9; ++i) {  // 因为固定的就是从 [1,9]中取数
            path.add(i);
            sum += i;
            traversal(k, n, i+1, sum);
            sum -= i;
            path.remove(path.size()-1);
        }
    }
}
